package com.metalrain.alm.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.metalrain.alm.ui.views.CameraPreview;
import com.philips.lighting.model.PHBridge;

public class LCA_Service extends Service {
    public HueManager mHueManager = new HueManager(this);
    public static final String BROADCAST_HUE_SERVICE_UPDATED = "CAM_HUE_SERVICE_UPDATED";
    private String errorMessage ="";
    private CameraPreview surface = null;
    private LightManager lightManager = new LightManager();

    public void setSurface(CameraPreview surface) {
        this.surface = surface;
    }


    public enum STATES{INITIALIZING, LOCATING, AUTHENTICATING, CONNECTED_TO_BRIDGE, ERROR};
    private STATES State =STATES.INITIALIZING;

    public LCA_Service() {}


    @Override
    public IBinder onBind(Intent intent) {
        return new LocalBinder();
    }


    @Override
    public void onCreate() {
        super.onCreate();
        initializeApplication();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        lightManager.stop();

    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void retry() {
        Log.i("LCA_Service", "retry");
        setState(STATES.INITIALIZING);
        initializeApplication();
    }


    public STATES getState() {
        return State;
    }

    public void setState(STATES state) {
        State = state;
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_HUE_SERVICE_UPDATED));
        if (State != STATES.CONNECTED_TO_BRIDGE) {
            lightManager.stop();
        }
    }

    public void startLightTasks() {
        PHBridge bridge = mHueManager.phHueSDK.getSelectedBridge();
        lightManager.start(bridge);
    }



    public class LocalBinder extends Binder {
        public LCA_Service getService() {
            return LCA_Service.this;
        }
    }



    private void initializeApplication() {
        Log.i("LCA_Service", "initializeApplication");
        if (getState() != STATES.INITIALIZING) return;
        if (haveNetworkConnection()) {
            setState(STATES.LOCATING);
            mHueManager.findHue();
        } else {
            setState(STATES.ERROR);
        }

    }

    private boolean haveNetworkConnection() {
        boolean isConnectedToLan = false;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo)
        {
            if (ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_ETHERNET)
                if (ni.isConnected())  isConnectedToLan = true;

        }
        return isConnectedToLan;
    }

}
